<?php

namespace Sharks\Core\Http\Form;

use Sharks\Core\AppContext;
use Sharks\Core\Cfg\AppConfig;
use Sharks\Core\Exception\FileUploadException;
use Sharks\Core\Support\Base\Argument;
use Sharks\Core\Support\Base\Arr;

/**
 * Http请求文件上传数据
 */
class FormFile {

	private $type;
	private $name;
	private $size;
	private $tmpName;
	private $error;
	private $ext;

	public function __construct($fileArr) {
		$this->type = $fileArr['type'];
		$this->name = $fileArr['name'];
		$this->size = $fileArr['size'];
		$this->tmpName = $fileArr['tmp_name'];
		$this->error = $fileArr['error'];

		// 获取上传文件扩展名
		$temp_arr = explode(".", $this->name);
		$file_ext = array_pop($temp_arr);
		$file_ext = trim($file_ext);
		$this->ext = strtolower($file_ext);
	}

	/**
	 * 是否上传有效，表示文件正确上传到服务器，且没有错误
	 * @return boolean [description]
	 */
	public function isValid() {
		return $this->error == UPLOAD_ERR_OK;// = 0
	}

	/**
	 *  
	 * @return [type] [description]
	 */
	public function type() {
		return $this->type;
	}

	public function size() {
		return $this->size;
	}

	public function name() {
		return $this->name;
	}

	public function error() {
		return $this->error;
	}

	/**
	 * 上传到服务器临时名称
	 * @return [type] [description]
	 */
	public function tmpName() {
		return $this->tmpName;
	}

	/**
	 * 是否在指定的文件类型中
	 * @param  array  $types [description]
	 * @return [type]        [description]
	 */
	public function isInType(array $types) {
		return isset($types[$this->type]);
	}

	public function ext() {
		return $this->ext;
	}

	/**
	 * 将表单中的文件依据新文件名，移动到指定位置
	 * @param $destination 指定位置，包括文件名与扩展名的绝对路径地址
	 */
	public function copy($destination) {
		move_uploaded_file($this->tmpName, $destination);
	}
}